Skip to content

Latest commit

 

History

History
140 lines (121 loc) · 6.35 KB

File metadata and controls

140 lines (121 loc) · 6.35 KB

Explore struct vs class vs actor differences in this in-depth article.

Table of Contents

  1. struct vs class vs actor - Comparison
  2. Structs
    1. Use Structures When...
  3. Classes
    1. Use Classes When...
  4. Actors
    1. Use Actors When...
    2. Do Not Use Actors When...
  5. References

struct vs class vs actor - Comparison

Structs Classes Actors
Type

Value type

Reference type

Reference type

Inheritance

Thread−safety

Memberwise Initializer

Deinitializer

Memory allocation

Stack

Heap

Heap

External access to public
properties and methods
direct

direct, but by using `await`

Protocols

  • conform to the `AnyObject`
  • can therefore conform to `Identifiable` without adding an explicit id property

  • conform to the `AnyObject`
  • can therefore conform to `Identifiable` without adding an explicit id property
  • conform to the `Actor`

Method execution
Can potentially be executing severals methods at a time

Can execute only one method at a time

Other

Can:

  • have initializers
  • have properties
  • have methods
  • have subscripts
  • have extensions
  • conform to protocols

Structs

  • It's a value type (along with e.g. arrays, dictionaries, enums and tuples).
    • Every time you use the struct reference in your code, it is potentially a new struct.
      • But Swift copy that new struct to a new memory address only if you modify the struct.
      • It called copy-on-write pattern. It's a memory optimization.
  • Structs cannot cause memory leaks.

Use Structures When...

  • Apple recommendation: use structures by default for storing data and modeling behavior.
  • When you don't need class' features:
    • You don't need to control the object's identity (use the identity operator ===).
    • You don't need a shared mutable state feature.
    • You don't need Objective-C interoperability.
    • You don't need class inheritance.
    • You need to replace class inheritance:
      • Protocols and structures can replace class inheritance.

Classes

  • It's a reference type.
  • Classes can cause memory leaks.

Use Classes When...

  • You need to control the object's identity (use the identity operator ===).
  • You need a shared mutable state feature.
  • You need Objective-C interoperability.
  • You need class inheritance.

Actors

  • It's a reference type.
  • Actors can cause memory leaks.

Use Actors When...

  • You need to use a reference type.
  • You need thread safety for shared mutable state.
    • It eliminates all kinds of race conditions and data races.

Do Not Use Actors When...

  • For your SwiftUI data models:
    • Instead use a class that conforms to the ObservableObject protocol.
      • If needed, you can optionally also mark that class with @MainActor to ensure it does any UI work safely,
      • But keep in mind that using @StateObject / @ObservedObject automatically makes a view’s code run on the main actor.
    • If you desperately need to be able to carve off some async work safely:
      • Create a sibling actor – a separate actor that does not use @MainActor, but does not directly update the UI.

References